#!/usr/bin/python -u
#
# Copyright (C) 2009 Chris Newton <redshodan@gmail.com>
#
# This file is part of remacs.
#
# remacs is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# remacs is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with remacs.  If not, see <http://www.gnu.org/licenses/>.
#
#
# Author: Chris Newton <redshodan@gmail.com>
# $Revision$
#

import sys
if ((sys.version_info[0] < 2) and (sys.version_info[1] < 5)):
    print "remacs requires Python version 2.5 or higher."
    sys.exit(-1)

### FIXME: Do this right once we have a setup.py and install etc.
import os
if os.path.islink(__file__):
    path = os.path.dirname(os.readlink(__file__))
else:
    path = os.path.dirname(__file__)
sys.path.append(os.path.abspath(os.path.join(path, "build/python")))

import sys, random
from optparse import OptionParser

import remacs
from remacs import log
from remacs.server import Server
from remacs.sslserver import SSLServerMgr
from remacs.client import Client
from remacs.sslclient import SSLClient


remacs.init(path)
del path


if __name__ == "__main__":
    parser = OptionParser(usage="Usage: %prog [options] <hostname>")
    parser.add_option("-s", "--server", action="store_true", dest="server",
                      default=False, help="Run in server mode")
    parser.add_option("-S", "--ssl", action="store_true", dest="ssl",
                      default=False, help="Run in ssl mode")
    parser.add_option("-H", "--host", dest="host", default="0.0.0.0",
                      help="Listen address for sslserver mode")
    parser.add_option("-P", "--sslport", dest="sslport",
                      default="4747", help="Sets ssl port")
    parser.add_option("-c", "--cert", dest="cert", default=None,
                      help="Cert for ssl mode")
    parser.add_option("-C", "--cacert", dest="cacert", default=None,
                      help="CA cert for ssl mode")
    parser.add_option("-t", "--transport", dest="transport", default="ssh -T",
                      help="Transport program. ie: ssh")
    parser.add_option("-T", "--term", dest="term", default=None,
                      help="Terminal type to use")
    parser.add_option("-X", "--no-x", action="store_true", dest="no_x",
                      default=False, help="Disable X support")
    parser.add_option("-V", "--no-vte", action="store_true", dest="no_vte",
                      default=False, help="Disable TTY window (VTE) support")
    parser.add_option("-i", "--id", dest="id",
                      default=str(int(random.random()*100)),
                      help="Set the id for this client")
    (options, args) = parser.parse_args()

    if not options.term:
        if "TERM" in os.environ:
            options.term = os.environ["TERM"]
        else:
            options.term = "xterm"

    if (options.ssl and (not options.cert or not options.cacert)):
        print "SSL mode requires both cert (-c) and cacert (-C) to be set."
        parser.print_help()
        sys.exit(1)

    if options.server:
        if options.ssl:
            log.init("sslserver")
            server = SSLServerMgr(options)
        else:
            log.init("server")
            server = Server(options)
        server.run()
    elif len(args) != 1:
        parser.print_help()
    else:
        options.host = args[0]
        if options.ssl:
            log.init("sslclient")
            client = SSLClient(options)
        else:
            log.init("client")
            client = Client(options)
        client.run()
